<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Illuminate\View\View;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;






//class AuthController extends BaseController
//{
//    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
//}


class AuthController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct()
    {
        $this->middleware('guest')->only('loginWeb');
        $this->middleware('throttle:60,1')->only(['loginWeb']);
    }

    public function loginWeb():View
    {
        return view('auth.login_web');
    }
    public function loginOpt(Request $request)
    {
        $data =  $this->validate(
            $request,[
            'email' => ['required', 'email'],
            'password' => ['required', 'min:6'],
        ]);
        if(Auth::attempt($data,$request->has('remember'))){


            if(Auth::user()->is_actived){
                session()->flash('success','登录成功，欢迎回来！');
                $default_url = route('users.show',Auth::user());
                return redirect()->intended($default_url);
            }else{
                Auth::logout();
                session()->flash('warning','你的账号还未激活，请通过邮箱激活账号！');
                return redirect('/');
            }

        }else{
            session()->flash('danger','登录失败，账号和密码不相匹配');
            return redirect()->back()->withInput();
        }
    }
    public function logout()
    {
        Auth::logout();
        session()->flash('success','已退出登录！');
        return redirect()->route('login');
    }
}
